package com.mrper.ext.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.miguo.ui.R;

/**
 * Created by Administrator on 2016/8/23.
 */

public class BrandSortView extends LinearLayout {

    /**
     * 未知模式
     **/
    public static final int MODE_NONE = -1;

    /**
     * 普通模式
     **/
    public static final int MODE_NORMAL = 0;

    /**
     * 从高到低
     **/
    public static final int MODE_B2S = 1;

    /**
     * 从低到高
     **/
    public static final int MODE_S2B = 2;

    /**
     * 默认
     **/
    public static final int SORT_DEFAULT = 0;

    /**
     * 新品
     **/
    public static final int SORT_NEW = 1;

    /**
     * 销量
     **/
    public static final int SORT_SALE_COUNT = 2;

    /**
     * 价格
     **/
    public static final int SORT_PRICE = 3;

    public interface OnSortChangedListener {
        void onSortChanged(int position, int sort, int mode);
    }

    private int sort = SORT_DEFAULT;
    private int mode = MODE_NONE;
    private int currentPositon = 0;
    private Context context;
    private TextView txtDefault;//默认
    private TextView txtNew;//新品
    private TextView txtSaleCount;//销量
    private TextView txtPrice;//价格
    private OnSortChangedListener onSortChangedListener;

    public BrandSortView(Context context) {
        super(context);
        init(context);
    }

    public BrandSortView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        addView(View.inflate(context, R.layout.widget_brandsort_view, null),
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        txtDefault = (TextView) findViewById(R.id.txtDefault);
        txtDefault.setOnClickListener(onClickListener);
        txtNew = (TextView) findViewById(R.id.txtNew);
        txtNew.setOnClickListener(onClickListener);
        txtSaleCount = (TextView) findViewById(R.id.txtSaleCount);
        txtSaleCount.setOnClickListener(onClickListener);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        txtPrice.setOnClickListener(onClickListener);
    }

    public void setOnSortChangedListener(OnSortChangedListener listener) {
        this.onSortChangedListener = listener;
    }

    public OnSortChangedListener getOnSortChangedListener() {
        return this.onSortChangedListener;
    }

    public void setCurrentPosition(int position) {
        if (this.currentPositon != position) {
            switch (position) {
                case 0:
                    txtDefault.performClick();
                    break;
                case 1:
                    txtSaleCount.performClick();
                    break;
                case 2:
                    txtPrice.performClick();
                    break;
            }
            this.currentPositon = position;
        }
    }

    public int getCurrentPositon(){
        return currentPositon;
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onSortChangedListener != null) {
                switch (v.getId()) {
                    case R.id.txtDefault:
                        currentPositon = 0;
                        if (sort != SORT_DEFAULT) {
                            sort = SORT_DEFAULT;
                            mode = MODE_NONE;
                            setTextViewState(txtDefault, true, false, MODE_NONE);
                            setTextViewState(txtNew, false, false, MODE_NONE);
                            setTextViewState(txtSaleCount, false, true, MODE_NORMAL);
                            setTextViewState(txtPrice, false, false, MODE_NORMAL);
                            onSortChangedListener.onSortChanged(currentPositon, sort, MODE_NONE);
                        }
                        break;
//                    case R.id.txtNew: //暂时去掉逻辑
//                        if (sort != SORT_NEW) {
//                            sort = SORT_NEW;
//                            mode = MODE_NONE;
//                            setTextViewState(txtDefault, false, false, MODE_NONE);
//                            setTextViewState(txtNew, true, false, MODE_NONE);
//                            setTextViewState(txtSaleCount, false, true, MODE_NORMAL);
//                            setTextViewState(txtPrice, false, false, MODE_NORMAL);
//                            onSortChangedListener.onSortChanged(currentPositon, sort, MODE_NONE);
//                        }
//                        break;
                    case R.id.txtSaleCount:
                        currentPositon = 1;
                        if (sort == SORT_PRICE)
                            mode = MODE_NONE;
                        sort = SORT_SALE_COUNT;
                        if (mode == MODE_NORMAL || mode == MODE_NONE) mode = MODE_B2S;
                        else if (mode == MODE_B2S) mode = MODE_S2B;
                        else mode = MODE_B2S;
                        setTextViewState(txtDefault, false, false, MODE_NONE);
                        setTextViewState(txtNew, false, false, MODE_NONE);
                        setTextViewState(txtSaleCount, true, true, mode);
                        setTextViewState(txtPrice, false, true, MODE_NORMAL);
                        onSortChangedListener.onSortChanged(currentPositon, sort, mode);
                        break;
                    case R.id.txtPrice:
                        currentPositon = 2;
                        if (sort == SORT_SALE_COUNT)
                            mode = MODE_NONE;
                        sort = SORT_PRICE;
                        if (mode == MODE_NORMAL || mode == MODE_NONE) mode = MODE_B2S;
                        else if (mode == MODE_B2S) mode = MODE_S2B;
                        else mode = MODE_B2S;
                        setTextViewState(txtDefault, false, false, MODE_NONE);
                        setTextViewState(txtNew, false, false, MODE_NONE);
                        setTextViewState(txtSaleCount, false, true, MODE_NORMAL);
                        setTextViewState(txtPrice, true, true, mode);
                        onSortChangedListener.onSortChanged(currentPositon, sort, mode);
                        break;
                }
            }
        }
    };

    /**
     * 设置TextView的状态
     *
     * @param txtView
     * @param isSelected
     * @param isHasDrawable
     * @param mode
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("deprecation")
    private void setTextViewState(TextView txtView, boolean isSelected, boolean isHasDrawable, int mode) {
        txtView.setTextColor(Color.parseColor(isSelected ? "#ffee4f4f" : "#ff333333"));
        if (isHasDrawable) {
            if (mode != MODE_NONE) {
                int imgId = mode == MODE_NORMAL ? R.drawable.ic_arr_ud_normal : (mode == MODE_B2S ? R.drawable.ic_arr_ud_u2p : R.drawable.ic_arr_ud_d2u);
                Drawable img = Build.VERSION.SDK_INT >= 22 ? context.getDrawable(imgId) : context.getResources().getDrawable(imgId);
                img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());
                txtView.setCompoundDrawables(null, null, img, null);
            } else {
                txtView.setCompoundDrawables(null, null, null, null);
            }
        }
    }

}
